package com.cskaoyan.javase.oop1._8call;

/**
 * 关于值传递:
 *      1.方法不可能改变其他方法中的局部变量
 *      2.方法可以改变传入方法的对象的状态
 *
 * @since 10:56
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo {
    public static void main(String[] args) {
        Student youngStudent = new Student(18);
        Student oldStudent = new Student(81);

        // 交换引用是不可能交换成功的
        swapReference(youngStudent, oldStudent);
        System.out.println(youngStudent.age);
        System.out.println(oldStudent.age);
        System.out.println("-------------------");

        swapAge(youngStudent, oldStudent);
        System.out.println(youngStudent.age);
        System.out.println(oldStudent.age);
    }

    // 交换引用
    public static void swapReference(Student s1, Student s2) {
        Student temp = s1;
        s1 = s2;
        s2 = temp;
    }

    // 交换年龄
    public static void swapAge(Student s1, Student s2) {
        int temp = s1.age;
        s1.age = s2.age;
        s2.age = temp;
    }
}

class Student {
    int age;

    public Student(int age) {
        this.age = age;
    }
}
